#include "serverlogwindow.h"
#include "ui_serverlogwindow.h"
#include "managewindow.h"
#include"QDebug"
#include<QFile>

#define serverPath "./data/serverIP.txt"
#define PROT 6065
serverLogWindow::serverLogWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::serverLogWindow)
{
    ui->setupUi(this);
    socket=new QTcpSocket;
    connect(socket,&QTcpSocket::connected,this,&serverLogWindow::connectedTohost);
    connect(socket,&QIODevice::readyRead,this,&serverLogWindow::readData);
    QFile *server=new QFile(serverPath);
   if(!server->open(QIODevice::ReadOnly))
   {
        qDebug()<<"open serverPath failed";
   }
  QByteArray arr=server->readAll();

    socket->connectToHost(QString(arr),PROT);


}

serverLogWindow::~serverLogWindow()
{
    delete ui;
}

void serverLogWindow::on_returnButton_clicked()
{
    QObject* before=this->parentWidget();
    manageWindow *p=dynamic_cast<manageWindow *>(before);
    p->show();
    this->close();
}

void serverLogWindow::on_confirmButton_clicked()
{
   QString startYear= ui->yearComboBox->currentText();
   QString startMonth=ui->monthComboBox->currentText();
   QString startDay=ui->dayComboBox->currentText();

   QString endYear=ui->yearComboBox_2->currentText();
   QString endMonth=ui->monthComboBox_2->currentText();
   QString endDay=ui->dayComboBox_2->currentText();

   QString startTime=(startYear+"-"+startMonth+"-"+startDay);
   QString endTime=(endYear+"-"+endMonth+"-"+endDay);


   QString data=QString("getLog#"+startTime+"#"+endTime);

   socket->write(data.toUtf8());
   //把此数据发送给服务器
   qDebug()<<data;

}

void serverLogWindow::connectedTohost()
{
     qDebug()<<"已连接到服务器";
}

void serverLogWindow::readData()
{
    QByteArray arr1=socket->readAll();
    ui->textBrowser->append(arr1);

}
